<?php
/**
 * 申请招聘
 */
apf_require_controller("Abstract");
apf_require_class("Bll_Hr_OutInterface");
apf_require_class("Bll_Flow_Recruit");
apf_require_class("Bll_Flow_Lumesse");
class Flow_ApplyRecruitController extends AbstractController {

    private $hroa_bll;
    private $user_dep;
    private $recruit_bll;

    public function handle_request() {
        $params = $this->request->get_parameters();
        $flow_id = empty($params['flow_id']) ? 0 : intval($params['flow_id']);
        $_luser = Bll_Flow_Lumesse::get_instance()->get_user_by_id($this->user_info['user_id']);
        if(empty($_luser)){
            $this->show_tips('ERROR_AUTH');
        }

        $this->hroa_bll = new Bll_Hr_OutInterface();
        $this->user_dep = $this->hroa_bll->get_user_base_info($this->user_info['user_id'],1);
        $this->recruit_bll = new Bll_Flow_Recruit();
        if($flow_id){
            $def_data = $this->recruit_bll->get_recruit_by_id($flow_id);
            if(empty($def_data) || $def_data['user_id'] != $this->user_info['user_id']){
                $this->show_tips('ERROR_PARAM');
            }
        }
        $this->log_bll = new Bll_Flow_OperateLog();
        if($this->request->is_post_method()){
            $this->_check_params($params);
            $def_data = $params;
            $new_flow_id = $flow_id;
            //有可能ceo招人
            $is_ceo = !empty($this->user_info['role_list'][Const_Flow::ROLE_CEO]) ? 1 : 0;
            if(!$flow_id){//add flow
                $new_flow_id = $this->_build_flow_data($params,$is_ceo);
            }
            $postdata = $this->_build_post_data($params,$new_flow_id,$flow_id);//有flow_id才是修改

            if($flow_id){//update
                $this->recruit_bll->update_recruit_by_id($flow_id,$postdata);
            }else{
                if(!$is_ceo){//ceo 招人，直接跳到hr审批
                    $super_uid = $this->_get_super_uid($this->user_info['user_id']);
                    if(!$super_uid){
                        $this->show_tips('ERROR_EMPTY_SUPER');
                    }
                    //add apply_relation
                    $rel_bll = new Bll_Flow_ApplyRelation();
                    $rel_bll->insert($new_flow_id,$this->user_info['user_id'],$super_uid,Const_Flow::FLOW_RECRUIT_2);
                }
                //add operator_log
                $this->_set_operator_log($new_flow_id,$postdata,$is_ceo);
                //add recruit
                $this->recruit_bll->add_recruit($postdata);
            }
            //update operator_log work_city_id
            $this->log_bll->update_city_id($flow_id,$params['city_id']);
            $this->response->redirect(Flow_Urls::flow_done_list());
        }
        //部门职位
        $def_out_data = array();
        if(!empty($def_data['job_id'])){
            $recruit_bll = new Bll_Flow_Recruit();
            $def_out_data = $recruit_bll->get_recruit_by_jobid($def_data['job_id']);
            $def_out_data['is_check'] = $def_out_data['anum']-$def_out_data['fnum'] <=0 ? 0 : $def_out_data['anum']-$def_out_data['fnum'];
        }
        $data['pm'] = $this->_get_pm_list($this->user_dep['p_level'],$this->user_dep['m_level']);

        //address
        $address = Const_Flow::office_address();
        if(empty($def_data['work_city_id'])){
            $data['office_address'] = $address[$this->user_dep['work_city_id']];
        }else{
            $data['office_address'] = $address[$def_data['work_city_id']];
        }
        $data['office_address_list'] = json_encode($address);
        $data['user_type'] = $this->_get_user_type();
        //核心能力
        $data['work_core_list'] = Const_Flow::work_core();
        $data['flow_id'] = $flow_id;

        $this->request->set_attribute('depdata',$this->user_dep);
        $this->request->set_attribute('def_data',$def_data);
        $this->request->set_attribute('def_out_data',$def_out_data);
        $this->request->set_attribute('data',$data);
        return 'Flow_ApplyRecruit';
    }
    /**
     * 取上级领导id
     * @param unknown_type $user_id
     * @return Ambigous <number, unknown>
     */
    private function _get_super_uid($user_id){
        $super_data = $this->hroa_bll->get_under_leader($user_id);
        return empty($super_data) ? 0 : $super_data['user_id'];
    }
    /**
     * ceo招人,需要写入3级审批的log
     * @param unknown_type $flow_id
     * @param unknown_type $data
     * @param unknown_type $is_ceo
     */
    private function _set_operator_log($flow_id,$data,$is_ceo){

        $t = date('Ymd');
        $log_data = array(
                'user_id'      => $this->user_info['user_id'],
                'user_name'    => $this->user_info['user_name'],
                'opt_user_id'  => $this->user_info['user_id'],
                'role_type'    => Const_Flow::ROLE_STAFF,
                'work_city_id' => $data['work_city_id'],
                'flow_id'      => $flow_id,
                'flow_opt'     => Const_Flow::PHASE_50,
                'flow_stage'   => Const_Flow::FLOW_RECRUIT_1,
                'flow_type'    => Const_Flow::FLOW_TYPE_RECRUIT,
                'effect_date'  => $t,
        );
        $this->log_bll->save_log($log_data);
        $opt_list = array(
                array(Const_Flow::PHASE_51,Const_Flow::FLOW_RECRUIT_2),
                array(Const_Flow::PHASE_52,Const_Flow::FLOW_RECRUIT_3),
                array(Const_Flow::PHASE_53,Const_Flow::FLOW_RECRUIT_4),
                array(Const_Flow::PHASE_70,Const_Flow::FLOW_RECRUIT_5),
                array(Const_Flow::PHASE_71,Const_Flow::FLOW_RECRUIT_6),
                array(Const_Flow::PHASE_72,Const_Flow::FLOW_RECRUIT_7),
                array(Const_Flow::PHASE_73,Const_Flow::FLOW_RECRUIT_8),
                array(Const_Flow::PHASE_74,Const_Flow::FLOW_RECRUIT_9),
                array(Const_Flow::PHASE_75,Const_Flow::FLOW_RECRUIT_10),
                array(Const_Flow::PHASE_76,Const_Flow::FLOW_RECRUIT_11),
        );
        if($is_ceo){
            for($i=0;$i<count($opt_list);$i++){
                $indata = array(
                        'user_id'      => $this->user_info['user_id'],
                        'user_name'    => $this->user_info['user_name'],
                        'opt_user_id'  => 0,
                        'role_type'    => Const_Flow::ROLE_SUPERIOR,
                        'work_city_id' => $data['work_city_id'],
                        'flow_id'      => $flow_id,
                        'flow_opt'     => $opt_list[$i][0],
                        'flow_stage'   => $opt_list[$i][1],
                        'flow_type'    => Const_Flow::FLOW_TYPE_RECRUIT,
                        'effect_date'  => $t,
                );
                $this->log_bll->save_log($indata);
            }
        }
        return true;
    }

    /**
     * flow data
     * @param array $params
     * @param is_ceo
     * @return boolean
     */
    private function _build_flow_data($params,$is_ceo=0){
        $flow_data = array(
                'user_id'=>$this->user_info['user_id'],
                'user_name'=>$this->user_info['user_name'],
                'flow_type'=>Const_Flow::FLOW_TYPE_RECRUIT,
                'flow_stage'=>Const_Flow::FLOW_RECRUIT_1,
                'flow_opt'=>Const_Flow::PHASE_50,
                'recruit'=>empty($params['out_plan_num']) ? 0 : 1,
                'flow_role_list'=>9,
                'work_city_id'=>$params['city_id'],
                'effect_date'=>date('Ymd'),
                'now_status'=>1
        );
        if($is_ceo){
            if($flow_data['recruit']){
                $flow_data['flow_stage'] = Const_Flow::FLOW_RECRUIT_11;
                $flow_data['flow_opt'] = Const_Flow::PHASE_76;
                $flow_data['flow_role_list'] = Const_Flow::ROLE_HRD;
            }else{
                $flow_data['flow_stage'] = Const_Flow::FLOW_RECRUIT_13;
                $flow_data['flow_opt'] = Const_Flow::PHASE_56;
                $flow_data['flow_role_list'] = Const_Flow::ROLE_RECRUIT;
            }
        }
        $flow_bll = new Bll_Flow_Info();
        return $flow_bll->insert_flow($flow_data);
    }
    /**
     * 构造post数据
     * @param array $params
     * @param int $flow_id有值就是修改
     * @param int $new_flow_id
     */
    private function _build_post_data($params,$new_flow_id=0,$flow_id=0){
        $indata = array(
            'job_id'=>intval($params['job_id']),
            'job_name'=>trim($params['job_name']),
            'job_needs'=>intval($params['job_needs']),
            'plan_num'=>intval($params['plan_num']),
            'out_plan_num'=>intval($params['out_plan_num']),
            'user_type'=>intval($params['user_type']),
            'is_p_m'=>intval($params['is_p_m']),
            'p_start'=>intval($params['p_start']),
            'p_end'=>intval($params['p_end']),
            'm_start'=>intval($params['m_start']),
            'm_end'=>intval($params['m_end']),
            'apply_reason'=>trim($params['apply_reason']),
            'work_address'=>trim($params['work_address']),
            'work_core'=>implode(',',$params['work_core']),
            'work_special'=>trim($params['work_special']),
            'work_plus'=>trim($params['work_plus']),
            'work_skilled'=>trim($params['work_skilled']),
            'is_written'=>trim($params['is_written']),
            'written_info'=>trim($params['written_info']),
            'work_city_id' => $params['city_id']
        );
        if($params['is_p_m']==1){
            $indata['m_start']=$indata['m_end']=0;
        }else{
            $indata['p_start']=$indata['p_end']=0;
        }
        if($params['is_written']==2){
            $indata['written_info']='';
        }
        if(!$flow_id){
            $indata['flow_id'] = $new_flow_id;
            $indata['user_id'] = $this->user_info['user_id'];
            $indata['department_id'] = $this->user_dep['department_id'];
            $indata['apply_status'] = 1;
            $indata['created'] = time();
            $indata['apply_last_opt'] = date('Ymd');
        }
        return $indata;
    }
    private function _check_params($params){
        if(empty($params['job_id']) || empty($params['city_id'])){
            $this->show_tips('ERROR_PARAM');
        }
        if($params['job_id']){
            $params['job_id'] = intval($params['job_id']);
            $job_data = $this->hroa_bll->get_job_dep_info($params['job_id']);
            if(empty($job_data)){
                $this->show_tips('ERROR_PARAM');
            }
        }
        if($params['city_id']){
            apf_require_class('Bll_Hr_Property');
            $proe_bll = new Bll_Hr_Property();
            $city_list = $proe_bll->get_sort_city_list();
            if(empty($city_list[$params['city_id']])){
                $this->show_tips('ERROR_PARAM');
            }
        }

        if(empty($params['job_needs']) || $params['job_needs'] < 0){
            $this->show_tips('ERROR_EMPTY_NEEDS');
        }
        if(empty($params['apply_reason'])){
            $this->show_tips('ERROR_EMPTY_REASON');
        }
        if(empty($params['work_core']) || count($params['work_core']) >3){
            $this->show_tips('ERROR_EMPTY_CORE');
        }
        if(empty($params['work_special'])){
            $this->show_tips('ERROR_EMPTY_SPECIAL');
        }
        if(empty($params['work_plus'])){
            $this->show_tips('ERROR_EMPTY_PLUS');
        }
        if(empty($params['work_skilled'])){
            $this->show_tips('ERROR_EMPTY_SKILL');
        }
        if($params['is_written']==1 && empty($params['written_info'])){
            $this->show_tips('ERROR_EMPTY_WRITE');
        }
    }
    //user type
    private function _get_user_type(){
        //$type = Const_User::get_all_user_type();
        return array(
                2 => array('type_id'=>2,'type_name'=>'正式员工'),
                3 => array('type_id'=>3,'type_name'=>'实习员工'),
                4 => array('type_id'=>4,'type_name'=>'兼职员工'),
        );
    }
    /**
     * 格式化pm下拉
     * @param int $p
     * @param int $m
     * @return multitype:
     */
    private function _get_pm_list($p,$m){
        $data = array(
                'p_list'=>array(),
                'm_list'=>array(),
        );
        if($p==0 && $m==0){
            return $data;
        }
        if($p){
            $plist = Const_Flow::turn_p_m();
            foreach($plist as $key=>$val){
                if($key <=$p){
                    $data['p_list'][$key] = $val;
                }
            }
            $data['m_list'] = Const_Flow::p_turn_m($p);
        }elseif($m){
            $mlist = Const_Flow::turn_p_m('m');
            foreach($mlist as $key=>$val){
                if($key <4){
                    continue;
                }
                $key -=3;
                if($key <=$m){
                    $data['m_list'][$key] = $val;
                }
            }
            $data['p_list'] = Const_Flow::m_turn_p($m);
        }
        return $data;
    }
    /**
     * 获取职位实际在编的人数，包括正在申请的人数
     */
    private function _get_fact_job_list($depid){
        $dep_job = $this->hroa_bll->get_department_jobs($depid);
        $job_ids = array_keys($dep_job);
        $job_data = $this->recruit_bll->get_recruit_by_jobs($job_ids);

        foreach ($dep_job as $job_id=>$val){
            if(!empty($job_data[$job_id])){
                $dep_job[$job_id]['anum'] = $val['fact_num']+$job_data[$job_id]['plan_num'];
                $dep_job[$job_id]['is_check'] = $job_data[$job_id]['out_plan_num'];
            }else{
                $dep_job[$job_id]['anum'] = $val['fact_num'];
                $dep_job[$job_id]['is_check'] = 0;
            }
        }
        return $dep_job;
    }
}